#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

void MyPerror(const char* msg)
{
  fprintf(stderr, "%s: %s\n", msg, strerror(errno));
}

int main()
{
  // 打开一个不存在的文件
  int fd = open("not_exist", O_RDONLY);
  if(fd < 0) {
    MyPerror("open");
    return 1;
  }
  close(fd);
  // 手动设置errno
  //errno=2;
  //perror("err");
  // stdout->1
  //printf("hello printf 1\n");
  //fprintf(stdout,"hello fprintf 1\n");
  //const char* s1 = "hello write1\n";
  //write(1, s1, strlen(s1));
  //std::cout<<"hello cout 1" << std::endl;
  //// stderr->2
  //perror("hello perror 2");
  //const char* s2 = "hello write 2\n";
  //write(2, s2, strlen(s2));
  //std::cerr<<"hello cerr 2" << std::endl;
  //close(1);
  //int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  //if(fd < 0) {
  //  perror("open");
  //  return 1;
  //}
  //printf("hello world, fd: %d\n", fd);
  //fflush(stdout);
  ////const char* msg = "hello world\n";
  ////write(fd, msg ,strlen(msg));
  //close(fd);
  return 0;
}
